home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / SYS / AMIGA / TTYIO.C < prev    next >
C/C++ Source or Header  |  1988-08-23  |  26KB  |  989 lines

  1. /*
  2.  * Name:    MG 2a
  3.  *        Amiga terminal window I/O, with all kinds o' trimmings.
  4.  *        This module is 'way too big.
  5.  * Last Edit:    01-Dec-87 mic@emx.cc.utexas.edu
  6.  * Created:    21-Apr-86 mic@emx.cc.utexas.edu
  7.  */
  8.  
  9. /*
  10.  * Lots of includes.
  11.  */
  12.  
  13. #include <exec/types.h>
  14. #include <exec/nodes.h>
  15. #include <exec/lists.h>
  16. #include <exec/tasks.h>
  17. #include <exec/ports.h>
  18. #include <exec/io.h>
  19. #include <devices/console.h>
  20. #include <devices/inputevent.h>
  21. #include <libraries/dos.h>
  22. #include <graphics/clip.h>
  23. #include <graphics/view.h>
  24. #include <graphics/rastport.h>
  25. #include <graphics/layers.h>
  26. #include <graphics/text.h>
  27. #include <graphics/gfxbase.h>
  28. #include <intuition/intuition.h>
  29. #include <intuition/intuitionbase.h>
  30. #include <libraries/diskfont.h>
  31.  
  32. #undef    TRUE            /* avoid redefinition messages         */
  33. #undef    FALSE
  34. #include "def.h"        /* includes sysdef.h and ttydef.h    */
  35.  
  36. /*
  37.  * External Amiga functions.
  38.  */
  39. extern    LONG             AbortIO();
  40. extern    LONG             CloseDevice();
  41. extern    LONG             CloseLibrary();
  42. extern    LONG             CloseWindow();
  43. extern    struct    MsgPort        *CreatePort();
  44. extern    struct    IOStdReq    *CreateStdIO();
  45. extern    LONG             DeletePort();
  46. extern    LONG             DeleteStdIO();
  47. extern    struct    IntuiMessage    *GetMsg();
  48. #ifndef    V11
  49. extern    LONG            GetScreenData();
  50. #endif
  51. extern    int             OpenConsole();
  52. extern    char            *OpenLibrary();
  53. extern    struct    Window        *OpenWindow();
  54. extern    struct TextFont        *OpenDiskFont();
  55. extern    LONG             RectFill();
  56. extern    LONG             ReplyMsg();
  57. extern    LONG             RawKeyConvert();
  58. extern    LONG             SetAPen();
  59. extern    LONG             SetDrMd();
  60. extern    LONG             Wait();
  61.  
  62. #ifdef    DO_MENU
  63. extern    LONG             ClearMenuStrip();    /* menu functions */
  64. extern    struct    Menu        *InitEmacsMenu();
  65. extern    struct    MenuItem    *ItemAddress();
  66. extern    LONG             SetMenuStrip();
  67. #endif
  68.  
  69. #ifdef    MANX
  70. extern    int    Enable_Abort;        /* Do NOT allow abort!        */
  71. #endif
  72.  
  73. /*
  74.  * External MG functions and variables
  75.  */
  76. extern    int    quit();            /* Defined by "main.c"        */
  77. extern    char    version[];        /* Version information        */
  78. extern    int    ttrow;            /* Current cursor row        */
  79. extern    int    use_metakey;        /* Do meta characters?        */
  80.  
  81. /*
  82.  * Non-int internal functions.  P?() is used to conditionally indicate
  83.  * ANSI-style prototype arguments for compilers (i.e. Lattice) that
  84.  * support them.
  85.  */
  86. #ifdef    LATTICE
  87. #define    P1(a)    a,
  88. #define    P2(a,b)    a,b
  89. #define    P3(a,b,c) a,b,c
  90. #else
  91. #define    P1(a)
  92. #define    P2(a,b)
  93. #define P3(a,b,c)
  94. #endif
  95.  
  96. VOID        panic(P1(char *));
  97. VOID        setttysize();
  98. VOID        ttclose();
  99. VOID        ttflush();
  100. VOID        ttnflush(P1(int));
  101. VOID        ttputc(P1(unsigned char));
  102.  
  103. static VOID    cleanup();
  104. static VOID    firstwin();
  105. static VOID    qkey(P1(KCHAR));
  106. #ifdef    DO_MENU
  107. static VOID    qmenu(P1(USHORT));
  108. #endif
  109. #ifdef    MOUSE
  110. static VOID    qmouse(P3(SHORT, SHORT, USHORT));
  111. #endif
  112. static VOID    ttreopen(P1(int)) ;
  113. static VOID    setmaxima() ;
  114. static struct Screen    *wbscreen();
  115.  
  116. /*
  117.  * Library bases (used by glue libraries)
  118.  */
  119.  
  120. struct    IntuitionBase    *IntuitionBase;
  121. struct    GfxBase        *GfxBase;
  122. ULONG            DiskfontBase;
  123.  
  124. /*
  125.  * Intuition window and menu variables.  MG gets used a lot, because it
  126.  * gets reconfigured on the fly for the amiga-set-font and toggle-border
  127.  * operations.
  128.  */
  129.  
  130. #define WINDOWGADGETS (WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE)
  131. #define WINDOWFLAGS    (WINDOWGADGETS | ACTIVATE)
  132.  
  133. struct NewWindow MG = {
  134.     0,    0,            /* start position           */
  135.     0,    0,            /* width, height (set by ttopen)*/
  136.     0,    1,                 /* detail pen, block pen    */
  137. #ifdef    DO_MENU
  138.     MENUPICK |            /* If menu is used        */
  139. #endif
  140. #ifdef    MOUSE
  141.     MOUSEBUTTONS |             /* If mouse is used        */
  142. #endif
  143.     INTUITICKS | RAWKEY |
  144.     CLOSEWINDOW | NEWSIZE,        /* IDCMP flags            */
  145.     0,                /* window flags    (set by ttopen)    */
  146.     NULL,                /* pointer to first user gadget */
  147.     NULL,                /* pointer to user checkmark    */ 
  148.     NULL,                /* title (filled in later)    */
  149.     NULL,                /* pointer to screen (none)    */
  150.     NULL,                /* pointer to superbitmap    */
  151.     220,40,                /* minimum size    (small!)    */
  152.     0, 0,                /* maximum size (set by ttopen)    */
  153.     WBENCHSCREEN            /* screen in which to open    */ 
  154. };
  155.  
  156. static short    borderless = TRUE;    /* Flag for borderless window    */
  157. static short    toggle_zooms = TRUE;    /* Does toggling border zoom?    */
  158. static int    last_top, last_left, last_height, last_width;
  159.  
  160. struct Window    *EmW = NULL;        /* Our window            */
  161. struct Screen    *EmS = NULL;        /* Our screen (usually WB)    */
  162. short        toggling = FALSE;    /* Prevent menu wiping        */
  163. #ifndef    V11
  164. struct Screen    WBInfo;            /* Info about the WB screen    */
  165. #endif
  166. struct TextFont *EmFont = NULL;        /* Our font (usually TOPAZ_xx)    */
  167.  
  168. #ifdef    DO_MENU
  169. static struct Menu    *EmacsMenu = NULL;    /* Our menu        */
  170. #endif
  171.  
  172. static ULONG        class;            /* Intuition event    */
  173. static USHORT        code,            /*   information    */
  174.             qualifier;
  175. static APTR        address;
  176. static SHORT        x, y;
  177. static LONG        intuitionMsgBit;    /* Signal bit        */
  178. #define INTUITION_MESSAGE ((LONG) (1L << intuitionMsgBit))
  179.  
  180. /* * * * * * * * * * * * * console I/O * * * * * * * * * * * * * * * * */
  181.  
  182. #define    CSI    0x9b            /* Command Sequence Introducer    */
  183. #define    NOBUF    512            /* About 1/4 screen        */
  184. #define    NIBUF    256            /* Input buffer            */
  185.  
  186. static KCHAR        ibuf[NIBUF];    /* keyboard input buffer    */
  187. static int        ibufo, nibuf;    /* head, # of bytes in ibuf    */
  188.  
  189. #ifndef    PROMPTWAIT
  190. #define    PROMPTWAIT 20            /* ticks to wait before timeout    */
  191. #endif
  192. static    LONG        tickcount;    /* # intuiticks    since last char    */
  193.  
  194. static struct MsgPort    *conWritePort = NULL;    /* I/O ports         */
  195. static struct IOStdReq    *conWriteMsg = NULL;    /* I/O messages        */
  196. struct Device        *ConsoleDevice;            /* used by RawKeyConvert*/
  197. static unsigned char    outbuf[NOBUF+7];    /* output buffer    */
  198. static unsigned char    *obuf;            /* first output char    */
  199. int            nobuf;            /* # of bytes in above    */
  200. int            nrow;            /* Terminal size, rows.    */
  201. int            ncol;            /* Terminal size, cols.    */
  202.  
  203. /* * * * * * * * * functions to open/reopen the window * * * * * * * * * */
  204.  
  205. /*
  206.  * Open up the virtual terminal MG communicates with. Set up the window,
  207.  * console, and menu strip.
  208.  */
  209.  
  210. ttopen()
  211. {
  212.  
  213. #ifdef    MANX
  214.     Enable_Abort = 0;                /* Disable ^C    */
  215. #endif
  216.  
  217.     /* firstwin() is only called the very first time we open the window */
  218.     if (toggling == FALSE)
  219.         firstwin();
  220.  
  221.     /* Set the window size, set the flags and title, and open it */
  222.  
  223.     setmaxima();
  224.     MG.Flags = WINDOWFLAGS;
  225.     MG.Flags |= borderless ? BORDERLESS : WINDOWSIZING;
  226.     MG.Title = (UBYTE *) &version[0];
  227.  
  228.     if ((EmW = OpenWindow(&MG)) == NULL)
  229.         cleanup();
  230.     SetFont(EmW->RPort, EmFont);
  231.  
  232.     /* Once the window is created, get the Intuition signal bit, set up
  233.      * the menu, and tell the virtual terminal how big it is.
  234.       */
  235.     setttysize();
  236.     intuitionMsgBit = EmW->UserPort->mp_SigBit;
  237. #ifdef    DO_MENU
  238.     if (toggling == FALSE)
  239.         EmacsMenu = InitEmacsMenu(EmW);
  240.     if (EmacsMenu == NULL)
  241.         cleanup();
  242.     SetMenuStrip(EmW, EmacsMenu);
  243. #endif
  244.  
  245.     /* Attach a console device (purely for output now) to our window
  246.      */
  247.  
  248.     if ((conWritePort = CreatePort("Emacs.con.write", 0L)) == NULL)
  249.         cleanup();
  250.     if ((conWriteMsg = CreateStdIO(conWritePort)) == NULL)
  251.         cleanup();
  252.  
  253.     if (OpenConsole(conWriteMsg,NULL,EmW) != 0)
  254.         cleanup();
  255.  
  256.     ConsoleDevice = conWriteMsg->io_Device;
  257.     nibuf = ibufo = 0;
  258.  
  259.     return (0);
  260. }
  261.  
  262. /*
  263.  * Set up the initial state of the window.  Opens up libraries, decides how
  264.  * big the initial window should be, and whether it should be borderless.
  265.  */
  266.  
  267. static VOID firstwin()
  268. {
  269.     GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L);
  270.     if (GfxBase == NULL)                /* Graphics lib    */
  271.         cleanup();
  272.  
  273.     IntuitionBase = (struct IntuitionBase *)     /* Intuition    */
  274.         OpenLibrary("intuition.library", 0L);
  275.     if (IntuitionBase == NULL)
  276.         cleanup();
  277.  
  278.     DiskfontBase = (ULONG) OpenLibrary("diskfont.library", 0L);
  279.     if (DiskfontBase == NULL)
  280.         cleanup();
  281.  
  282.     /* Get our screen and font, then figure out if we can go borderless
  283.     */
  284.     if ((EmS = wbscreen()) == NULL)
  285.         cleanup();
  286.     EmFont = OpenDiskFont(EmS->Font);
  287.     if ((EmS->Width >= ((INIT_COLS * EmFont->tf_XSize) + LR_BORDER)) &&
  288.         (EmS->Height >= ((INIT_ROWS * EmFont->tf_YSize) + TB_BORDER)))
  289.         borderless = FALSE;
  290.  
  291.     /* Set the size of the initial window and fake the last one
  292.      */
  293.     last_wid